package com.jrx.anytxn.param.service.account.impl;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.param.dto.account.GaProductSummaryInfoRes;
import com.jrx.anytxn.param.entity.GaProductSummaryInfo;
import com.jrx.anytxn.param.mapper.ext.ExtGaProductSummaryInfoMapper;
import com.jrx.anytxn.param.service.account.IGaProductSummaryInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 余额总账汇总表service实现
 *
 * @author yf
 * date 2020/4/16 0016
 * @version V1.0
 **/
@Service
public class GaProductSummaryInfoServiceImpl implements IGaProductSummaryInfoService {

    private static final Logger logger = LoggerFactory.getLogger(GaProductSummaryInfoServiceImpl.class);

    @Resource
    private ExtGaProductSummaryInfoMapper extGaProductSummaryInfoMapper;


    @Override
    public TxnPage<GaProductSummaryInfoRes> getPageByCondition(Integer pageNum, Integer pageSize, String tenantId,String productId,String orgId ,String beginDate, String endDate) throws TxnException {
        Integer startIndex=(pageNum-1)*(pageSize/3);
        Integer size=pageSize/3;
        List<GaProductSummaryInfo> productSummaryInfoList = extGaProductSummaryInfoMapper.selectAllByPageAndTenId(tenantId,productId,orgId,beginDate,endDate,startIndex,size);
        Integer selectCount=extGaProductSummaryInfoMapper.selectCount(tenantId,productId,orgId,beginDate,endDate);
        List<GaProductSummaryInfoRes> productSummaryInfoResList=new ArrayList<>();
        if (productSummaryInfoList.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }

        for (GaProductSummaryInfo gaProductSummaryInfo:productSummaryInfoList) {

            GaProductSummaryInfoRes gaProductSummaryInfoRes1=new GaProductSummaryInfoRes();
            GaProductSummaryInfoRes gaProductSummaryInfoRes2=new GaProductSummaryInfoRes();
            GaProductSummaryInfoRes gaProductSummaryInfoRes3=new GaProductSummaryInfoRes();
            gaProductSummaryInfoRes1.setProductId(gaProductSummaryInfo.getProductId());
            gaProductSummaryInfoRes1.setBussinessDate(gaProductSummaryInfo.getBussinessDate());
            gaProductSummaryInfoRes1.setOrganizationId(gaProductSummaryInfo.getOrganizationId());
            gaProductSummaryInfoRes1.setCatacory("分户");
            gaProductSummaryInfoRes1.setNomalPrinBal(gaProductSummaryInfo.getNomalPrinBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getNomalPrinBal());
            gaProductSummaryInfoRes1.setOverDuePrinBal(gaProductSummaryInfo.getOverDuePrinBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getOverDuePrinBal());
            gaProductSummaryInfoRes1.setDeadPrinBal(gaProductSummaryInfo.getDeadPrinBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getDeadPrinBal());
            gaProductSummaryInfoRes1.setBadPrinBal(gaProductSummaryInfo.getBadPrinBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getBadPrinBal());
            gaProductSummaryInfoRes1.setNomalIntrBal(gaProductSummaryInfo.getNomalIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getNomalIntrBal());
            gaProductSummaryInfoRes1.setOverDueIntrBal(gaProductSummaryInfo.getOverDueIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getOverDueIntrBal());
            gaProductSummaryInfoRes1.setPentIntrBal(gaProductSummaryInfo.getPentIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getPentIntrBal());
            gaProductSummaryInfoRes1.setCompIntrBal(gaProductSummaryInfo.getCompIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getCompIntrBal());
            gaProductSummaryInfoRes1.setAccuIntrBal(gaProductSummaryInfo.getAccuIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccuIntrBal());
            gaProductSummaryInfoRes1.setAccuPentIntrBal(gaProductSummaryInfo.getAccuPentIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccuPentIntrBal());
            gaProductSummaryInfoRes1.setAccuCompIntrBal(gaProductSummaryInfo.getAccuCompIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccuCompIntrBal());

            gaProductSummaryInfoRes2.setProductId(gaProductSummaryInfo.getProductId());
            gaProductSummaryInfoRes2.setBussinessDate(gaProductSummaryInfo.getBussinessDate());
            gaProductSummaryInfoRes2.setOrganizationId(gaProductSummaryInfo.getOrganizationId());
            gaProductSummaryInfoRes2.setCatacory("总账");
            gaProductSummaryInfoRes2.setNomalPrinBal(gaProductSummaryInfo.getAccountingNomalPrinBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingNomalPrinBal());
            gaProductSummaryInfoRes2.setOverDuePrinBal(gaProductSummaryInfo.getAccountingOverDuePrinBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingOverDuePrinBal());
            gaProductSummaryInfoRes2.setDeadPrinBal(gaProductSummaryInfo.getAccountingDeadPrinBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingDeadPrinBal());
            gaProductSummaryInfoRes2.setBadPrinBal(gaProductSummaryInfo.getAccountingBadPrinBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingBadPrinBal());
            gaProductSummaryInfoRes2.setNomalIntrBal(gaProductSummaryInfo.getAccountingNomalIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingNomalIntrBal());
            gaProductSummaryInfoRes2.setOverDueIntrBal(gaProductSummaryInfo.getAccountingOverDueIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingOverDueIntrBal());
            gaProductSummaryInfoRes2.setPentIntrBal(gaProductSummaryInfo.getAccountingPentIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingPentIntrBal());
            gaProductSummaryInfoRes2.setCompIntrBal(gaProductSummaryInfo.getAccountingCompIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingCompIntrBal());
            gaProductSummaryInfoRes2.setAccuIntrBal(gaProductSummaryInfo.getAccountingAccuIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingAccuIntrBal());
            gaProductSummaryInfoRes2.setAccuPentIntrBal(gaProductSummaryInfo.getAccountingAccuPentIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingAccuPentIntrBal());
            gaProductSummaryInfoRes2.setAccuCompIntrBal(gaProductSummaryInfo.getAccountingAccuCompIntrBal()==null?BigDecimal.ZERO:gaProductSummaryInfo.getAccountingAccuCompIntrBal());

            gaProductSummaryInfoRes3.setProductId(gaProductSummaryInfo.getProductId());
            gaProductSummaryInfoRes3.setBussinessDate(gaProductSummaryInfo.getBussinessDate());
            gaProductSummaryInfoRes3.setOrganizationId(gaProductSummaryInfo.getOrganizationId());
            gaProductSummaryInfoRes3.setCatacory("差额");
            gaProductSummaryInfoRes3.setNomalPrinBal(gaProductSummaryInfoRes1.getNomalPrinBal().subtract(gaProductSummaryInfoRes2.getNomalPrinBal()));
            gaProductSummaryInfoRes3.setOverDuePrinBal(gaProductSummaryInfoRes1.getOverDuePrinBal().subtract(gaProductSummaryInfoRes2.getOverDuePrinBal()));
            gaProductSummaryInfoRes3.setDeadPrinBal(gaProductSummaryInfoRes1.getDeadPrinBal().subtract(gaProductSummaryInfoRes2.getDeadPrinBal()));
            gaProductSummaryInfoRes3.setBadPrinBal(gaProductSummaryInfoRes1.getBadPrinBal().subtract(gaProductSummaryInfoRes2.getBadPrinBal()));
            gaProductSummaryInfoRes3.setNomalIntrBal(gaProductSummaryInfoRes1.getNomalIntrBal().subtract(gaProductSummaryInfoRes2.getNomalIntrBal()));
            gaProductSummaryInfoRes3.setOverDueIntrBal(gaProductSummaryInfoRes1.getOverDueIntrBal().subtract(gaProductSummaryInfoRes2.getOverDueIntrBal()));
            gaProductSummaryInfoRes3.setPentIntrBal(gaProductSummaryInfoRes1.getPentIntrBal().subtract(gaProductSummaryInfoRes2.getPentIntrBal()));
            gaProductSummaryInfoRes3.setCompIntrBal(gaProductSummaryInfoRes1.getCompIntrBal().subtract(gaProductSummaryInfoRes2.getCompIntrBal()));
            gaProductSummaryInfoRes3.setAccuIntrBal(gaProductSummaryInfoRes1.getAccuIntrBal().subtract(gaProductSummaryInfoRes2.getAccuIntrBal()));
            gaProductSummaryInfoRes3.setAccuPentIntrBal(gaProductSummaryInfoRes1.getAccuPentIntrBal().subtract(gaProductSummaryInfoRes2.getAccuPentIntrBal()));
            gaProductSummaryInfoRes3.setAccuCompIntrBal(gaProductSummaryInfoRes1.getAccuCompIntrBal().subtract(gaProductSummaryInfoRes2.getAccuCompIntrBal()));

            productSummaryInfoResList.add(gaProductSummaryInfoRes1);
            productSummaryInfoResList.add(gaProductSummaryInfoRes2);
            productSummaryInfoResList.add(gaProductSummaryInfoRes3);


        }
        return new TxnPage<>(pageNum, pageSize, selectCount, productSummaryInfoResList);
    }
}
